package titt.io.encoding;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import titt.type.Type;

public class ObjectEncoder implements Encoder {

	@Override
	public byte[] encode(Type t, Object o) {
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		try {
			
			ObjectOutputStream oos = new ObjectOutputStream(bout);
			oos.writeObject(o);
			oos.flush();
			
			return bout.toByteArray();
		}
		catch (IOException e) {}

		return new byte[0];
	}

	@Override
	public boolean isEncodeable(Type t) {
		return t.isNative();
	}
}
